import unittest
import os,sys
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),'../'))

from DataGet import WikiLinkYear

class TestStringMethods(unittest.TestCase):
    #每个测试类继承于unittest.TestCase类

    def setUp(self):
        pass
    #每个testXXX函数运行前会先运行setUp函数

    def tearDown(self):
        pass
     #每个testXXX函数运行后会运行tearDown函数

    #每个测试函数必须以test开头,否则不会被当成测试函数
    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

    def test_WikiLinkYear_edges(self):
        years = [i for i in range(2007,2020)]
        del(years[years.index(2012)])
        del(years[years.index(2010)])
        for year in years:
            wly = WikiLinkYear(year)
            count = 0
            for a,b in wly.iterate_edges():
                if count <5:
                    print(year,a,b)
                count += 1
            print(year,count)

    def test_WikiLinkYear_nodes(self):
        years = [i for i in range(2008,2020)]
        del(years[years.index(2012)])
        for year in years:
            wly = WikiLinkYear(year)
            a,b = wly.get_article_ids()
            self.assertEqual(len(a),len(b))
            for k,v in a.items():
                self.assertEqual(len(v),len(b[k]))
                if len(v) == 0:
                    print(year,k)

        wly = WikiLinkYear(2007)
        a,b = wly.get_article_ids()
        self.assertEqual(a['Environmental engineering'][0],24872)
        self.assertEqual(a['Environmental engineering'][1],65424)
        self.assertEqual(a['Environmental engineering'][2],13435)
        self.assertEqual(a['Environmental engineering'][3],165921)
        self.assertEqual(a['Environmental engineering'][4],27745)

#使本py文件可以直接$ python test.py执行测试
if __name__ == '__main__':
    unittest.main()